/*
 * ApplicationConfiguration.java
 *
 * Created on March 27, 2005, 12:32 AM
 */

package tools;

import java.awt.Component;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Keith
 */
public class ApplicationConfiguration {
  
	/**
	 *
	 */
  private static byte[] lock = new byte[1];

	/**
	 *
	 */
  private static ApplicationConfiguration instance;

	/**
	 *
	 */
  private Properties props;

	/**
	 *
	 */
  private String configFilename;

  /**
   * Associates a name to an IconImage.
   */
  private Map<String,Icon> icons;

  /**
   * Used for the selection of various files.
   */
  private JFileChooser fileChooser;

  /**
   * This filter works with the file chooser to limit the files shown.<p>
   *
   * Only files with an extension of <code><i>.xml</i></code> are displayed.
   */
  static final public FileFilter xmlFilter = new FileFilter(){
    public boolean accept(File pathname){
      return pathname.isDirectory() || pathname.getName().endsWith( ".xml" );
    }

    public String getDescription(){
      return "XML File (*.xml)";
    }
  };

  /**
   * This filter works with the file chooser to limit the files shown.<p>
   *
   * Only files with an extension of <code><i>.xsl</i></code> are displayed.
   */
  static final public FileFilter xslFilter = new FileFilter(){
    public boolean accept(File pathname){
      return pathname.isDirectory() || pathname.getName().endsWith( ".xsl" );
    }

    public String getDescription(){
      return "XSL File (*.xsl)";
    }
  };

	/**
	 *
	 */
  protected ApplicationConfiguration( Object app ) {
    icons = new HashMap<String,Icon>();
    props = new Properties();

    String appClassName = app.getClass().getName();

    int lastDot = appClassName.lastIndexOf( '.' );
    configFilename = "/" + appClassName.substring( lastDot + 1 ) + ".properties";
//System.out.println( configFilename );

    URL configUrl = getClass().getResource( configFilename );
//System.out.println( configUrl );
    if ( configUrl != null ){
      InputStream is = null;

      try {
        is = configUrl.openStream();
        props.load( is );
      } catch ( IOException ex ){
      } finally {
        if ( is != null ){
          try { is.close(); } catch (Exception ex ){}
        }
      }
    }
  }

	/**
	 *
	 */
	public JFileChooser getFileChooser(){
		if ( fileChooser == null ){
			String classPath = System.getProperty( "java.class.path" );
			StringTokenizer token = new StringTokenizer( classPath, File.pathSeparator );
			File pathDir = null;
			while ( token.hasMoreTokens() && pathDir == null ){
				pathDir = new File( token.nextToken() );
				if ( pathDir != null && pathDir.isDirectory() ){
					fileChooser = new JFileChooser( pathDir );
				} else {
					fileChooser = new JFileChooser( );
				}
			}
		}

		return fileChooser;
	}

  /**
   * A blank 16x16 Icon.  Intended for use within JMenuItems as a place holder
   * so that menu text is properly aligned.
   */
  private Icon blankIcon = new Icon(){
    public void paintIcon(Component c, Graphics g, int x, int y){}

    public int getIconWidth(){	return 16; }

    public int getIconHeight(){	return 16; }
  };

	/**
	 *
	 */
  public Icon getIcon( String name ){
    Icon icon = icons.get( name );

    if ( icon == null ){
      String iconUrlName = props.getProperty( name + ".icon" );
      if ( iconUrlName != null ){
        URL iconUrl = getClass().getResource( iconUrlName );
        if ( iconUrl != null ){
          icon = new ImageIcon( iconUrl );
        }
      }

      if ( icon == null ){
        icon = blankIcon;
      }

      icons.put( name, icon );
    }

    return icon;
  }

	/**
	 *
	 */
  public static ApplicationConfiguration getInstance( Object app ){
    if ( instance == null ){
      synchronized ( lock ){
        if ( instance == null ){
          instance = new ApplicationConfiguration( app );
        }

        lock.notifyAll();
      }
    }

    return instance;
  }

	/**
	 *
	 */
  public static ApplicationConfiguration getInstance(){
		return instance;
  }
}
